<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>学生成绩管理系统 - 成绩查询</title>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/css/all.min.css" rel="stylesheet">
    <style>
        .sidebar {
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            z-index: 100;
            padding: 48px 0 0;
            box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
            background-color: #f8f9fa;
        }
        .sidebar-sticky {
            position: relative;
            top: 0;
            height: calc(100vh - 48px);
            padding-top: .5rem;
            overflow-x: hidden;
            overflow-y: auto;
        }
        .navbar {
            box-shadow: 0 2px 4px rgba(0,0,0,.1);
        }
        .main-content {
            margin-left: 240px;
            padding: 20px;
        }
        .nav-link {
            color: #333;
            padding: 10px 20px;
        }
        .nav-link:hover {
            background-color: #e9ecef;
        }
        .nav-link.active {
            background-color: #007bff;
            color: white;
        }
        .nav-link i {
            margin-right: 10px;
        }
        .chart-container {
            height: 400px;
            margin-bottom: 20px;
        }
        .grade-card {
            transition: transform 0.2s;
        }
        .grade-card:hover {
            transform: translateY(-5px);
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">学生成绩管理系统</a>
            <div class="d-flex">
                <span class="navbar-text me-3">
                    欢迎，${sessionScope.user.username}
                </span>
                <button class="btn btn-outline-light" onclick="logout()">退出</button>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row">
            <nav class="col-md-3 col-lg-2 d-md-block sidebar">
                <div class="sidebar-sticky">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" href="index.jsp">
                                <i class="fas fa-home"></i>首页
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="grade.jsp">
                                <i class="fas fa-chart-bar"></i>成绩查询
                            </a>
                        </li>
                    </ul>
                </div>
            </nav>

            <main class="main-content">
                <div class="container">
                    <h2 class="mb-4">成绩查询</h2>
                    <div class="row">
                        <div class="col-md-4">
                            <div class="card grade-card bg-primary text-white mb-4">
                                <div class="card-body">
                                    <h5 class="card-title">期中成绩</h5>
                                    <h2 id="midGrade">-</h2>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card grade-card bg-success text-white mb-4">
                                <div class="card-body">
                                    <h5 class="card-title">期末成绩</h5>
                                    <h2 id="endGrade">-</h2>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card grade-card bg-info text-white mb-4">
                                <div class="card-body">
                                    <h5 class="card-title">平均成绩</h5>
                                    <h2 id="avgGrade">-</h2>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card mb-4">
                        <div class="card-body">
                            <h5 class="card-title">成绩趋势</h5>
                            <div id="gradeChart" class="chart-container"></div>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">成绩详情</h5>
                            <div class="table-responsive">
                                <table class="table">
                                    <thead>
                                        <tr>
                                            <th>学期</th>
                                            <th>期中成绩</th>
                                            <th>期末成绩</th>
                                            <th>平均成绩</th>
                                            <th>状态</th>
                                        </tr>
                                    </thead>
                                    <tbody id="gradeDetails">
                                        <!-- 成绩详情将通过JavaScript动态加载 -->
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/bootstrap.bundle.min.js"></script>
    <script>
        function logout() {
            $.post('${pageContext.request.contextPath}/user/logout', function() {
                window.location.href = '${pageContext.request.contextPath}/login.jsp';
            });
        }

        // 加载成绩信息
        function loadGradeInfo() {
            $.get('${pageContext.request.contextPath}/student/${sessionScope.user.username}', function(student) {
                // 更新成绩卡片
                $('#midGrade').text(student.midMarks || '-');
                $('#endGrade').text(student.endMarks || '-');
                
                // 计算平均成绩
                const midMarks = student.midMarks || 0;
                const endMarks = student.endMarks || 0;
                const avgGrade = midMarks && endMarks ? ((midMarks + endMarks) / 2).toFixed(2) : '-';
                $('#avgGrade').text(avgGrade);

                // 更新成绩详情表格
                $('#gradeDetails').html(`
                    <tr>
                        <td>${student.semester}</td>
                        <td>${student.midMarks || '-'}</td>
                        <td>${student.endMarks || '-'}</td>
                        <td>${avgGrade}</td>
                        <td>${student.status}</td>
                    </tr>
                `);

                // 初始化成绩趋势图表
                initGradeChart(student);
            });
        }

        // 初始化成绩趋势图表
        function initGradeChart(student) {
            const chartDom = document.getElementById('gradeChart');
            const myChart = echarts.init(chartDom);
            
            const option = {
                title: {
                    text: '成绩分布'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['期中成绩', '期末成绩', '平均成绩']
                },
                xAxis: {
                    type: 'category',
                    data: ['成绩']
                },
                yAxis: {
                    type: 'value',
                    min: 0,
                    max: 100
                },
                series: [
                    {
                        name: '期中成绩',
                        type: 'bar',
                        data: [student.midMarks || 0]
                    },
                    {
                        name: '期末成绩',
                        type: 'bar',
                        data: [student.endMarks || 0]
                    },
                    {
                        name: '平均成绩',
                        type: 'line',
                        data: [student.midMarks && student.endMarks ? 
                            (student.midMarks + student.endMarks) / 2 : 0]
                    }
                ]
            };

            myChart.setOption(option);
        }

        // 页面加载完成后加载数据
        $(document).ready(function() {
            loadGradeInfo();
        });
    </script>
</body>
</html> 